/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:CommonAction.java
 * Version:1.0
 * Date:2011-12-23
 * Description: 公共操作
 * author:zhijian.zhang
 */
package com.tietiao.action.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tietiao.common.action.BaseAction;
import com.tietiao.common.json.JsonControllerHelper;
import com.tietiao.common.util.StringUtil;
import com.tietiao.dao.model.Tietiao;
import com.tietiao.dao.model.UserCommidityFavorite;
import com.tietiao.service.tietiao.CommidityFavoriteService;
import com.tietiao.service.tietiao.TietiaoService;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class CommonAction extends BaseAction {
	@Resource
	private TietiaoService tietiaoService;
	@Resource
	private CommidityFavoriteService commidityFavoriteService;
	// 商品ID数组
	private String[] commodityIds;
	
	// 商品ID
	private String commodityId;
	
	// 描述
	private String description;
	// 收藏夹ID
	private String favoriteId;
	// 用户收藏夹列表
	private List<UserCommidityFavorite> userCommidityFavorites;
	// 收藏夹名称
	private String favoriteName;
	
	public String getCommodityId() {
		return commodityId;
	}
	public void setCommodityId(String commodityId) {
		this.commodityId = commodityId;
	}
	public String getFavoriteName() {
		return favoriteName;
	}
	public void setFavoriteName(String favoriteName) {
		this.favoriteName = favoriteName;
	}
	public List<UserCommidityFavorite> getUserCommidityFavorites() {
		return userCommidityFavorites;
	}
	public void setUserCommidityFavorites(
			List<UserCommidityFavorite> userCommidityFavorites) {
		this.userCommidityFavorites = userCommidityFavorites;
	}
	/**
	 * @return the commodityIds
	 */
	public String[] getCommodityIds() {
		return commodityIds;
	}
	/**
	 * @param commodityIds the commodityIds to set
	 */
	public void setCommodityIds(String[] commodityIds) {
		this.commodityIds = commodityIds;
	}
	/**
	 * @return the favoriteId
	 */
	public String getFavoriteId() {
		return favoriteId;
	}

	/**
	 * @param favoriteId the favoriteId to set
	 */
	public void setFavoriteId(String favoriteId) {
		this.favoriteId = favoriteId;
	}

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	

	/**
	 * 
	 * 功能：贴条操作
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-23
	 * 
	 * @return
	 * @throws Exception
	 */
	public String tietiao() throws Exception {

		Map<String, String> result = new HashMap<String, String>();
		if (commodityIds == null || commodityIds.length == 0) {
			result.put("success", "false");
			result.put("msg", "未选择需要贴条的商品！");
			JsonControllerHelper.writeJsonToResponse(response, result);
			return null;
		}
		List<Tietiao> tietiaos = new ArrayList<Tietiao>();
		int userId = getUser().getUserId();
		int favId = Integer.parseInt(favoriteId);
		
		for (int i = 0, len = commodityIds.length; i < len; i++) {
			Tietiao tietiao = new Tietiao();
			tietiao.setCommodityId(Integer.parseInt(commodityIds[i]));
			tietiao.setFavoriteId(favId);
			tietiao.setIsShare(0);
			tietiao.setUserDesc(description);
			tietiao.setUserId(userId);
			tietiaos.add(tietiao);
		}
		tietiaoService.tietiao(tietiaos);
		result.put("success", "true");
		result.put("msg", "success");
		JsonControllerHelper.writeJsonToResponse(response, result);
		return null;
	}

	/**
	 * 
	 * 功能：撕条操作
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-23
	 * 
	 * @return
	 */
	public String shitiao() throws Exception {
		Map<String, String> result = new HashMap<String, String>();
		if (commodityIds == null || commodityIds.length == 0) {
			result.put("success", "false");
			result.put("msg", "未选择需要贴条的商品！");
			JsonControllerHelper.writeJsonToResponse(response, result);
			return null;
		}
		tietiaoService.shitiao(commodityIds, getUser().getUserId());
		result.put("success", "true");
		result.put("msg", "success");
		JsonControllerHelper.writeJsonToResponse(response, result);
		return null;
	}
	/**
	 * 
	 * 功能：处理异常
	 * @author: zhijian.zhang
	 * @Date: 2012-1-5
	 *
	 * @return
	 * @throws Exception
	 */
	public String handleError() throws Exception {
		return dispatcher("/jsp/fg/exception/error.jsp");
	}
	
	/**
	 * 
	 * 功能：显示贴条层
	 * @author: zhijian.zhang
	 * @Date: 2012-1-5
	 *
	 * @return
	 * @throws Exception
	 */
	public String showTietiao() throws Exception {
		userCommidityFavorites = commidityFavoriteService.getUserCommidityFavorites(getUser().getUserId(), "FAVORITE_ID ASC");
		
		if(userCommidityFavorites == null || userCommidityFavorites.size() == 0) {
			UserCommidityFavorite userCommidityFavorite = new UserCommidityFavorite();
			userCommidityFavorite.setCreateTime(new Date());
			userCommidityFavorite.setDefaultType(1);
			userCommidityFavorite.setFavoriteName("未分类");
			userCommidityFavorite.setUserId(getUser().getUserId());
			commidityFavoriteService.addUserCommidityFavorite(userCommidityFavorite);
			userCommidityFavorites = new ArrayList<UserCommidityFavorite>();
			userCommidityFavorites.add(userCommidityFavorite);
		}
		return dispatcher("/jsp/fg/commodity/tietiao.jsp");
	}
	
	/**
	 * 
	 * 功能：添加收藏夹
	 * @author: zhijian.zhang
	 * @Date: 2012-1-6
	 *
	 * @return
	 * @throws Exception
	 */
	public String addFav() throws Exception {
		response.setContentType("text/html");
		if(StringUtil.strIsNull(favoriteName)) {
			response.getWriter().append("{'suc':'false','msg':'收藏名称不能为空'}");
			return null;
		}
		UserCommidityFavorite userCommidityFavorite = new UserCommidityFavorite();
		userCommidityFavorite.setCreateTime(new Date());
		userCommidityFavorite.setDefaultType(0);
		userCommidityFavorite.setFavoriteName(favoriteName);
		userCommidityFavorite.setUserId(getUser().getUserId());
		commidityFavoriteService.addUserCommidityFavorite(userCommidityFavorite);
		response.getWriter().append("{'suc':'true','msg':'"+userCommidityFavorite.getFavoriteId()+"'}");
		return null;
	}

}
